|
1
|
|
|
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */ |
|
2
|
|
|
import XLSX from 'xlsx'; |
|
3
|
|
|
|
|
4
|
|
|
import React, { Component } from 'react'; |
|
5
|
|
|
import { AppRegistry, StyleSheet, Text, View, Button, Alert, Image, ScrollView, TouchableWithoutFeedback } from 'react-native'; |
|
6
|
|
|
import { Table, Row, Rows, TableWrapper } from 'react-native-table-component'; |
|
7
|
|
|
|
|
8
|
|
|
// react-native-fs |
|
9
|
|
|
import { writeFile, readFile, DocumentDirectoryPath } from 'react-native-fs'; |
|
10
|
|
|
const DDP = DocumentDirectoryPath + "/"; |
|
11
|
|
|
const input = res => res; |
|
12
|
|
|
const output = str => str; |
|
13
|
|
|
|
|
14
|
|
|
// react-native-fetch-blob |
|
15
|
|
|
/* |
|
16
|
|
|
import RNFetchBlob from 'react-native-fetch-blob'; |
|
17
|
|
|
const { writeFile, readFile, dirs:{ DocumentDir } } = RNFetchBlob.fs; |
|
18
|
|
|
const DDP = DocumentDir + "/"; |
|
19
|
|
|
const input = res => res.map(x => String.fromCharCode(x)).join(""); |
|
20
|
|
|
const output = str => str.split("").map(x => x.charCodeAt(0)); |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
const make_cols = refstr => Array.from({length: XLSX.utils.decode_range(refstr).e.c + 1}, (x,i) => XLSX.utils.encode_col(i)); |
|
24
|
|
|
const make_width = refstr => Array.from({length: XLSX.utils.decode_range(refstr).e.c + 1}, () => 60); |
|
25
|
|
|
|
|
26
|
|
|
export default class SheetJS extends Component { |
|
27
|
|
|
constructor(props) { |
|
28
|
|
|
super(props); |
|
29
|
|
|
this.state = { |
|
30
|
|
|
data: [[1,2,3],[4,5,6]], |
|
31
|
|
|
widthArr: [60, 60, 60], |
|
32
|
|
|
cols: make_cols("A1:C2") |
|
33
|
|
|
}; |
|
34
|
|
|
this.importFile = this.importFile.bind(this); |
|
35
|
|
|
this.exportFile = this.exportFile.bind(this); |
|
36
|
|
|
}; |
|
37
|
|
|
importFile() { |
|
38
|
|
|
Alert.alert("Rename file to sheetjs.xlsx", "Copy to " + DDP, [ |
|
39
|
|
|
{text: 'Cancel', onPress: () => {}, style: 'cancel' }, |
|
40
|
|
|
{text: 'Import', onPress: () => { |
|
41
|
|
|
readFile(DDP + "sheetjs.xlsx", 'ascii').then((res) => { |
|
42
|
|
|
/* parse file */ |
|
43
|
|
|
const wb = XLSX.read(input(res), {type:'binary'}); |
|
44
|
|
|
|
|
45
|
|
|
/* convert first worksheet to AOA */ |
|
46
|
|
|
const wsname = wb.SheetNames[0]; |
|
47
|
|
|
const ws = wb.Sheets[wsname]; |
|
48
|
|
|
const data = XLSX.utils.sheet_to_json(ws, {header:1}); |
|
49
|
|
|
|
|
50
|
|
|
/* update state */ |
|
51
|
|
|
this.setState({ data: data, cols: make_cols(ws['!ref']), widthArr: make_width(ws['!ref']) }); |
|
52
|
|
|
}).catch((err) => { Alert.alert("importFile Error", "Error " + err.message); }); |
|
53
|
|
|
}} |
|
54
|
|
|
]); |
|
55
|
|
|
} |
|
56
|
|
|
exportFile() { |
|
57
|
|
|
/* convert AOA back to worksheet */ |
|
58
|
|
|
const ws = XLSX.utils.aoa_to_sheet(this.state.data); |
|
59
|
|
|
|
|
60
|
|
|
/* build new workbook */ |
|
61
|
|
|
const wb = XLSX.utils.book_new(); |
|
62
|
|
|
XLSX.utils.book_append_sheet(wb, ws, "SheetJS"); |
|
63
|
|
|
|
|
64
|
|
|
/* write file */ |
|
65
|
|
|
const wbout = XLSX.write(wb, {type:'binary', bookType:"xlsx"}); |
|
66
|
|
|
const file = DDP + "sheetjsw.xlsx"; |
|
67
|
|
|
writeFile(file, output(wbout), 'ascii').then((res) =>{ |
|
68
|
|
|
Alert.alert("exportFile success", "Exported to " + file); |
|
69
|
|
|
}).catch((err) => { Alert.alert("exportFile Error", "Error " + err.message); }); |
|
70
|
|
|
}; |
|
71
|
|
|
render() { return ( |
|
72
|
|
|
<ScrollView contentContainerStyle={styles.container} vertical={true}> |
|
73
|
|
|
<Text style={styles.welcome}> </Text> |
|
74
|
|
|
<Image style={{width: 128, height: 128}} source={require('./logo.png')} /> |
|
75
|
|
|
<Text style={styles.welcome}>SheetJS React Native Demo</Text> |
|
76
|
|
|
<Text style={styles.instructions}>Import Data</Text> |
|
77
|
|
|
<Button onPress={this.importFile} title="Import data from a spreadsheet" color="#841584" /> |
|
78
|
|
|
<Text style={styles.instructions}>Export Data</Text> |
|
79
|
|
|
<Button disabled={!this.state.data.length} onPress={this.exportFile} title="Export data to XLSX" color="#841584" /> |
|
80
|
|
|
|
|
81
|
|
|
<Text style={styles.instructions}>Current Data</Text> |
|
82
|
|
|
|
|
83
|
|
|
<ScrollView style={styles.table} horizontal={true} > |
|
84
|
|
|
<Table style={styles.table}> |
|
85
|
|
|
<TableWrapper> |
|
86
|
|
|
<Row data={this.state.cols} style={styles.thead} textStyle={styles.text} widthArr={this.state.widthArr}/> |
|
87
|
|
|
</TableWrapper> |
|
88
|
|
|
<TouchableWithoutFeedback> |
|
89
|
|
|
<ScrollView vertical={true}> |
|
90
|
|
|
<TableWrapper> |
|
91
|
|
|
<Rows data={this.state.data} style={styles.tr} textStyle={styles.text} widthArr={this.state.widthArr}/> |
|
92
|
|
|
</TableWrapper> |
|
93
|
|
|
</ScrollView> |
|
94
|
|
|
</TouchableWithoutFeedback> |
|
95
|
|
|
</Table> |
|
96
|
|
|
</ScrollView> |
|
97
|
|
|
</ScrollView> |
|
98
|
|
|
); }; |
|
99
|
|
|
}; |
|
100
|
|
|
|
|
101
|
|
|
const styles = StyleSheet.create({ |
|
102
|
|
|
container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF' }, |
|
103
|
|
|
welcome: { fontSize: 20, textAlign: 'center', margin: 10 }, |
|
104
|
|
|
instructions: { textAlign: 'center', color: '#333333', marginBottom: 5 }, |
|
105
|
|
|
thead: { height: 40, backgroundColor: '#f1f8ff' }, |
|
106
|
|
|
tr: { height: 30 }, |
|
107
|
|
|
text: { marginLeft: 5 }, |
|
108
|
|
|
table: { width: "100%" } |
|
109
|
|
|
}); |
|
110
|
|
|
|
|
111
|
|
|
AppRegistry.registerComponent('SheetJS', () => SheetJS); |
|
112
|
|
|
|